package com.ruoyi.wq.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.wq.mapper.WqDynamicMapper;
import com.ruoyi.wq.mapper.WqMiniGameMapper;
import com.ruoyi.wq.domain.WqDynamic;
import com.ruoyi.wq.domain.WqMiniGame;
import com.ruoyi.wq.service.IWqMiniGameService;

import cn.hutool.core.date.DateUtil;

/**
 * 微信小游戏Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-16
 */
@Service
public class WqMiniGameServiceImpl extends ServiceImpl<WqMiniGameMapper, WqMiniGame> implements IWqMiniGameService {

    @Autowired
    private WqMiniGameMapper wqMiniGameMapper;
    
    @Autowired
    private WqDynamicMapper wqDynamicMapper;

    /**
     * 查询微信小游戏
     * 
     * @param id 微信小游戏主键
     * @return 微信小游戏
     */
    @Override
    public WqMiniGame selectWqMiniGameById(String id)
    {
        return wqMiniGameMapper.selectWqMiniGameById(id);
    }

    /**
     * 查询微信小游戏列表
     * 
     * @param wqMiniGame 微信小游戏
     * @return 微信小游戏
     */
    @Override
    public List<WqMiniGame> selectWqMiniGameList(WqMiniGame wqMiniGame)
    {
        return wqMiniGameMapper.selectWqMiniGameList(wqMiniGame);
    }

    /**
     * 新增微信小游戏
     * 
     * @param wqMiniGame 微信小游戏
     * @return 结果
     */
    @Override
    public int insertWqMiniGame(WqMiniGame wqMiniGame)
    {
        wqMiniGame.setCreateTime(DateUtils.getNowDate());
        return wqMiniGameMapper.insertWqMiniGame(wqMiniGame);
    }

    /**
     * 修改微信小游戏
     * 
     * @param wqMiniGame 微信小游戏
     * @return 结果
     */
    @Override
    public int updateWqMiniGame(WqMiniGame wqMiniGame)
    {
        wqMiniGame.setUpdateTime(DateUtils.getNowDate());
        return wqMiniGameMapper.updateWqMiniGame(wqMiniGame);
    }

    /**
     * 批量删除微信小游戏
     * 
     * @param ids 需要删除的微信小游戏主键
     * @return 结果
     */
    @Override
    public int deleteWqMiniGameByIds(String[] ids)
    {
        return wqMiniGameMapper.deleteWqMiniGameByIds(ids);
    }

    /**
     * 删除微信小游戏信息
     * 
     * @param id 微信小游戏主键
     * @return 结果
     */
    @Override
    public int deleteWqMiniGameById(String id)
    {
        return wqMiniGameMapper.deleteWqMiniGameById(id);
    }

	@Override
	public TableDataInfo getPageList(WqMiniGame wqMiniGame) {
		IPage<WqMiniGame> page = baseMapper.selectPageList(Condition.getPage(wqMiniGame), wqMiniGame);
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public List<String> getQrCode(Long id) {
		WqMiniGame game = baseMapper.selectById(id);
		Long playNum = game.getPlayNum();
		Long eyeNum = game.getEyeNum();
		game.setPlayNum(playNum+1);
		game.setEyeNum(eyeNum+1);
		baseMapper.updateById(game);
		
		WqDynamic dynamic = wqDynamicMapper.selectById(game.getAdId());
		if(dynamic != null){
			dynamic.setCreateTime(DateUtil.date());
			wqDynamicMapper.updateById(dynamic);
		}
		List<String> qrCode = new ArrayList<String>();
		qrCode.add(game.getQrCode());
		return qrCode;
	}

	@Override
	public String getAppId(Long id) {
		WqMiniGame game = baseMapper.selectById(id);
		Long eyeNum = game.getEyeNum();
		game.setEyeNum(eyeNum+1);
		baseMapper.updateById(game);
		
		WqDynamic dynamic = wqDynamicMapper.selectById(game.getAdId());
		if(dynamic != null){
			dynamic.setCreateTime(DateUtil.date());
			wqDynamicMapper.updateById(dynamic);
		}
		return game.getAppId();
	}
}
